<template>
  <div class="container">
    <Header title="修改密码"></Header>
    <mt-field label="原始密码："
              type="password"
              placeholder="请输入(必填)"
              v-model="oldPwd"></mt-field>
    <mt-field label="新密码："
              type="password"
              placeholder="请输入(必填)"
              v-model="newPwd"></mt-field>
    <mt-field label="确认新密码："
              type="password"
              placeholder="请输入(必填)"
              style="border-bottom: 1px solid #d9d9d9"
              v-model="confrmpwd"></mt-field>
    <div class="report-button">
      <mt-button size="large"
                 type="danger"
                 @click="saveBtn">保存</mt-button>
    </div>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      oldPwd: "",
      newPwd: "",
      confrmpwd: ""
    };
  },
  methods: {
    saveBtn() {
      var _self = this;
      if (_self.pwdValidate() == true) {
        var body = {};
        body.params = {
          oldPassword: _self.oldPwd,
          newPassword: _self.newPwd,
          confirmPassword: _self.confrmpwd
        };
        var url = this.utilHelper.apiUrl + "/api/updatePwd";
        _self.$http.post(url, body).then((response) => {
          var body = response.body;
          if (body.result == 1) {
            let instance = Toast("修改成功");
            setTimeout(() => {
              instance.close();
              _self.quit();
            }, 2000);
          } else if (body.result == 0) {
            Toast({
              message: body.resData.resultMsg,
              position: "middle",
              duration: 2000
            });
          }
        });
      }
    },
    pwdValidate() {
      var _self = this;
      var oldPwd = _self.oldPwd;
      var newPwd = _self.newPwd;
      var confrmpwd = _self.confrmpwd;
      var pwdReg = /^.{8,20}$/;
      if (!oldPwd) {
        Toast({
          message: "原密码不能为空",
          position: "middle",
          duration: 3000
        });
        return false;
      }
      if (!pwdReg.test(newPwd)) {
        Toast({
          message: "新密码长度必须在8~20位之间!",
          position: "middle",
          duration: 3000
        });
        return false;
      }
      if (newPwd != confrmpwd) {
        Toast({
          message: "两次密码输入不一致!",
          position: "middle",
          duration: 3000
        });
        return false;
      }
      return true;
    },
    quit() {
      var _self = this;
      var body = {};
      var params = { userId: window.localStorage.getItem("2_userId") };
      body.params = params;
      var url = _self.utilHelper.apiUrl + "/api/loginOut";
      _self.$http.post(url, body).then(
        (response) => {
          var data = response.body;
          window.sessionStorage.setItem(
            _self.utilHelper.appAssistant + "_isLogin",
            false
          );
          _self.$router.replace({
            path: "/login"
          });
        },
        (response) => {}
      );
    }
  }
};
</script>
<style scoped>
.report-button {
  padding: 0 10px;
}
.report-button .mint-button--danger {
  background-color: #eb6100;
  margin-top: 20px;
}
.digital-input:after {
  content: "";
  display: block;
  clear: both;
  visibility: hidden;
  overflow: hidden;
  font-size: 0;
  height: 0;
}
</style>